package org.spring.springboot.app.community.notice.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.community.notice.dao.CommunityNoticeDao;
import org.spring.springboot.app.community.notice.domain.po.CommunityNoticePO;
import org.spring.springboot.app.community.notice.domain.vo.CommunityNoticeVO;
import org.spring.springboot.app.community.notice.domain.vo.NoticeSearchVO;
import org.spring.springboot.app.community.notice.service.ICommunityNoticeService;
import org.spring.springboot.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CommunityNoticeServiceImpl implements ICommunityNoticeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CommunityNoticeServiceImpl.class);

    @Autowired
    private CommunityNoticeDao communityNoticeDao;

    // key生成器,redis生成key必须用
    @Autowired
    private RedisUtils redisUtils;


    public void removeSysDictCache() {
        redisUtils.delete("dict");
    }

    @Override
    public CommunityNoticePO find(String id) {
        return communityNoticeDao.selectByPrimaryKey(id);
    }

    @Override
    public List<CommunityNoticeVO> findPage(NoticeSearchVO searchvo, int pageNum, int pageSize,String officeId) {
        return communityNoticeDao.findPage(searchvo,pageNum, pageSize,officeId);
    }

    @Override
    public int save(CommunityNoticePO po) {
        po.preInsert();
        int i = communityNoticeDao.insertSelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int update(CommunityNoticePO po) {
        po.preUpdate();
        int i = communityNoticeDao.updateByPrimaryKeySelective(po);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }

    @Override
    public int delete(String id) {
        CommunityNoticePO syCnoticePO = new CommunityNoticePO();
        syCnoticePO.setId(id);
        syCnoticePO.preDelete();
        int i = communityNoticeDao.updateByPrimaryKeySelective(syCnoticePO);
        if (i > 0) {
            this.removeSysDictCache();
        }
        return i;
    }
    @Override
    public int deletes(String ids) {
        CommunityNoticePO syCnoticePO = new CommunityNoticePO();
        String[] id_s = ids.split(",");
        syCnoticePO.preDelete();
        int count = 0;
        for (String id : id_s) {
            syCnoticePO.setId(id);
            count += communityNoticeDao.updateByPrimaryKeySelective(syCnoticePO);
        }
        return count;
    }
}
